package com.dmlll.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dmlll.VO.ProviderCloseVO;
import com.dmlll.VO.PurchaseVO;
import com.dmlll.common.DataGridView;
import com.dmlll.common.ResultObj;
import com.dmlll.domain.*;
import com.dmlll.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author dmLll
 * @since 2021-04-13
 */
@RestController
@RequestMapping("/providerCloseDetail")
public class ProviderCloseDetailController {
    @Autowired
    private ProviderCloseService providerCloseService;
    @Autowired
    private ProviderCloseDetailService providerCloseDetailService;
    @Autowired
    private DrugMessageService drugMessageService;
    @Autowired
    private ProviderService providerService;
    @Autowired
    private PurchaseService purchaseService;
    @Autowired
    private PurchaseDrupDetailService purchaseDrupDetailService;
    @Autowired
    private HospitalService hospitalService;
    @RequestMapping("getAllClose")
    public DataGridView getAllClose(ProviderCloseVO providerClose, ProviderCloseDetail providerCloseDetail, DrugMessage drugMessage,PurchaseVO purchase) {
        //查询结算单
        QueryWrapper<ProviderClose> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("provider_id", providerClose.getProviderId());
        queryWrapper.eq("close_form_state",2);//已提交的结算单
        queryWrapper.like(StringUtils.isNotBlank(providerClose.getCloseNum()), "close_num", providerClose.getCloseNum());
        queryWrapper.like(StringUtils.isNotBlank(providerClose.getCloseName()), "close_name", providerClose.getCloseName());
        queryWrapper.ge(providerClose.getStartTime() != null, "create_close_time", providerClose.getStartTime());
        queryWrapper.le(providerClose.getEndTime() != null, "create_close_time", providerClose.getEndTime());
        List<ProviderClose> providerCloseList = providerCloseService.list(queryWrapper);
        //获取退货单编号集合
        List<String> outputNumList = new ArrayList<>();
        for (ProviderClose output : providerCloseList) {
            outputNumList.add(output.getCloseNum());//存在则有退货单
        }
        if (outputNumList.size() > 0) {
            //获得退货单详情
            QueryWrapper<ProviderCloseDetail> qw = new QueryWrapper<>();
            qw.in("close_num", outputNumList)
                    .eq("close_state", providerCloseDetail.getCloseState());
            List<ProviderCloseDetail> list = providerCloseDetailService.list(qw);
            List<ProviderCloseDetail> resultList = new ArrayList<>();
            //填充数据
            for (ProviderCloseDetail pod : list) {
                QueryWrapper<DrugMessage> w1 = new QueryWrapper<>();
                w1.eq("id", pod.getDrugMessageId())
                        .like(StringUtils.isNotBlank(drugMessage.getSerialNumber()), "serial_number", drugMessage.getSerialNumber())
                        .like(StringUtils.isNotBlank(drugMessage.getCommonName()), "common_name", drugMessage.getCommonName())
                        .like(StringUtils.isNotBlank(drugMessage.getDosageForm()), "dosage_form", drugMessage.getDosageForm())
                        .like(StringUtils.isNotBlank(drugMessage.getSize()), "size", drugMessage.getSize())
                        .like(StringUtils.isNotBlank(drugMessage.getUnit()), "unit", drugMessage.getUnit())
                        .eq(drugMessage.getFactor() != null, "factor", drugMessage.getFactor())
                        .like(StringUtils.isNotBlank(drugMessage.getProductionName()), "production_name", drugMessage.getProductionName())
                        .like(StringUtils.isNotBlank(drugMessage.getCommodityName()), "commodity_name", drugMessage.getCommodityName())
                        .eq(drugMessage.getDrugCategory() != null && drugMessage.getDrugCategory() > 0, "drug_category", drugMessage.getDrugCategory())
                        .eq(drugMessage.getQuality() != null && drugMessage.getQuality() > 0, "quality", drugMessage.getQuality());
                pod.setDrugMessage(drugMessageService.getOne(w1));
                QueryWrapper<Purchase> w2 = new QueryWrapper<>();
                w2.eq("id", providerClose.getProviderId())
                        .eq(purchase.getHospitalId() != null, "hospital_id", purchase.getHospitalId())
                        .ge(purchase.getCreateTime() != null, "create_time", purchase.getCreateTime())
                        .le(purchase.getOverTime() != null, "over_time", purchase.getOverTime())
                        .like(StringUtils.isNotBlank(purchase.getPurchaseNum()), "purchase_num", purchase.getPurchaseNum())
                        .like(StringUtils.isNotBlank(purchase.getPurchaseName()), "purchase_name", purchase.getPurchaseName());
                Purchase purchase1 = purchaseService.getOne(w2);
                if (purchase1 != null) {
                    purchase1.setHospitalName(hospitalService.getById(purchase1.getHospitalId()).getHospitalName());
                }
                pod.setPurchase(purchase1);
                pod.setProvider(providerService.getById(providerClose.getProviderId()));

                pod.setProviderClose(providerCloseService.getOne(new QueryWrapper<ProviderClose>().
                        eq("close_num", pod.getCloseNum())));
                pod.setPurchaseDrupDetail(purchaseDrupDetailService.getOne(new QueryWrapper<PurchaseDrupDetail>().
                        eq("drug_message_id", pod.getDrugMessageId()).
                        eq("purchase_id", pod.getPurchaseId())));
                if (pod.getDrugMessage() != null && pod.getPurchase() != null) {
                    resultList.add(pod);
                }
            }
            return new DataGridView(Long.valueOf(resultList.size()), resultList);
        }
        return new DataGridView();
    }
    @RequestMapping("update")
    public ResultObj update(String close){
        try {
            providerCloseDetailService.updateBatchById(JSON.parseArray(close,ProviderCloseDetail.class));
        } catch (Exception e) {
            return ResultObj.UPDATE_ERROR;
        }
        return ResultObj.UPDATE_SUCCESS;
    }
}

